package com.wedo.mall.activity.agent;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.wedo.mall.R;
import com.wedo.mall.activity.login.WLogin;
import com.wedo.mall.dialog.ShareDialog;
import com.wedo.mall.entity.Agent;
import com.wedo.mall.widget.EWebView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * 店铺简介
 * Created by e on 2017/7/19.
 */

public class AgentIntroActivity extends EActivity {

    @BindView(R.id.ll_web) LinearLayout ll_web;
    @BindView(R.id.iv_collection) ImageView iv_collection;
    private EWebView eWebView;
    private Agent agent;
    private String collect_id;

    @Override
    protected int layout() {
        return R.layout.activity_agent_intr;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        disableSwipeBack();
        eWebView = new EWebView(ll_web);

        agent = getIntent().getParcelableExtra(AgentDetailActivity.EXTRA_AGENT);
        intro(agent.id);
    }

    @Override
    protected void onPause() {
        super.onPause();
        isCorE();
    }

    private void intro(String id){
        loading();
        Map<String,String> params = new HashMap<>(1);
        params.put("id",id);
        Observable<EResponse> observable = api().post(EApi.AGENT_INTRO,params);
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                display(response);
            }

            @Override
            public void failure(String msg) {
                dismiss();
            }
        });

    }

    private void display(EResponse response){
        Map<String,String> map = response.object("list",Map.class);
        if (map !=null){
            String intro = map.get("introduction");
            eWebView.data(intro);
            collect_id = map.get("collect_id");
            String is_collect = map.get("is_collect");
            iv_collection.setSelected(TextUtils.equals("1",is_collect));
        }
    }

    @OnClick(R.id.iv_backward)
    protected void onBackwardClick(){
        setResult();
    }

    @OnClick(R.id.iv_share)
    protected void onShareClick(){
        ShareDialog.with(this).agent(agent).show();
    }

    @OnClick(R.id.iv_collection)
    protected void onCollectionClick(){
        if(iv_collection.isSelected()) {
            delCollect();
        }else {
            toCollect();
        }
    }

    private void toCollect(){
        if(!WLogin.isLogged(this)) {
            return;
        }

        loading();

        String id = agent.id;
        String type = "1";
        Observable<EResponse> observable = api().collect(id,type);
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                iv_collection.setSelected(true);
                collect_id = response.string("collect_id");
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    private void delCollect(){
        loading();
        Observable<EResponse> observable = api().delCollect(collect_id);
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                iv_collection.setSelected(false);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    private void setResult(){
        Intent intent = new Intent();
        intent.putExtra("collect_id",collect_id);
        intent.putExtra("is_collect",iv_collection.isSelected());
        setResult(RESULT_OK,intent);
        defaultFinish();
    }

    @Override
    protected void onDestroy() {
        eWebView.onDestroy();
        super.onDestroy();
    }
}
